<?php

namespace app\common;

use think\Config;

class QrcodeServer{

    public function qrcode($text, $filename='', $size=300, $level=3)
    {
        Vendor('phpqrcode.phpqrcode');
        //生成二维码图片
        $object = new \QRcode();
        $filename = empty($filename) ? time() : $filename;
        $outfile = 'resources/qrcode/'.$filename.'.jpg';
        $arr = $object->png($text,  $outfile, $level, $size, 2);
        return $arr;
    }


    public function qrcodeLogo($text, $filename='', $logo='', $size = 270)
    {
//        带LOGO
        $filename = empty($filename) ? time() : $filename;
        $url = $text;
        $errorCorrectionLevel = 'L';//容错级别
        //生成二维码图片
        Vendor('phpqrcode.phpqrcode');
        $object = new \QRcode();
        $ad = 'resources/qrcode/'.$filename.'.jpg';
        $object->png($url, $ad, $errorCorrectionLevel, $size, 2);
        $QR = 'resources/qrcode/'.$filename.'.jpg';//已经生成的原始二维码图

        if ($logo) {
            $QR = imagecreatefromstring(file_get_contents($QR));
            $logo = imagecreatefromstring(file_get_contents($logo));
            $QR_width = imagesx($QR);//二维码图片宽度
            $QR_height = imagesy($QR);//二维码图片高度
            $logo_width = imagesx($logo);//logo图片宽度
            $logo_height = imagesy($logo);//logo图片高度
            $logo_qr_width = $QR_width / 6;
            $scale = $logo_width/$logo_qr_width;
            $logo_qr_height = $logo_height/$scale;
            $from_width = ($QR_width - $logo_qr_width) / 2;
            //重新组合图片并调整大小
            imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
            imagepng($QR, 'resources/qrcode/'.$filename.'.png');
            unlink(ROOT_PATH.'/'.$ad);
        }
        $outfile = $logo ? $filename.'.png' : $filename.'.jpg';
        return $outfile;
    }

    public function combineimg($backimg, $upimg, $toimg)
    {
        if( empty($backimg) || empty($upimg) || empty($toimg) ) return false;
        $upimg = 'resources/qrcode/'.$upimg;
        //创建图片的实例
        $dst = imagecreatefromstring(file_get_contents($backimg));
        $src = imagecreatefromstring(file_get_contents($upimg));
        // 把前图片的白色背景设为透明
        imagecolortransparent($src, imagecolorallocate($src, 255, 255, 255));

        //获取水印图片的宽高
        list($src_w, $src_h) = getimagesize($upimg);

        //将水印图片复制到目标图片上
        imagecopymerge($dst, $src, 215, 755, 0, 0, $src_w, $src_h, 100);

        //生成图片
        imagepng($dst, 'resources/qrcode/'.$toimg);//销毁
        imagedestroy($dst);
        imagedestroy($src);
        return $toimg;
    }

    public function setimg($filename, $per=0.1, $tofile)
    {
        if( $per > 1 ) return false;
        $filename = 'resources/qrcode/'.$filename;
        list($width, $height, $type) = getimagesize($filename);
        $n_w = $width*$per;
        $n_h = $height*$per;
        $new = imagecreatetruecolor($n_w, $n_h);
        switch($type){
            case 1:
                $img=imagecreatefromgif($filename);
                break;
            case 2:
                $img=imagecreatefromjpeg($filename);
                break;
            case 3:
                $img=imagecreatefrompng($filename);
                break;
        }
        //拷贝部分图像并调整
        imagecopyresized($new, $img,0, 0,0, 0, $n_w, $n_h, $width, $height);
        //图像输出新图片、另存为
        imagepng($new, 'resources/qrcode/'.$tofile);
        imagedestroy($new);
        imagedestroy($img);
        return $tofile;
    }



}


